﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCSLibrary;
using Utils;
using Utils.DC.Web.UI;
using Utils.DatabaseUtils;

public partial class Admin_Form_Contact_Add : System.Web.UI.Page
{
    CategoryPageUI objCategoryPage = new CategoryPageUI();
    ContactUI objContact = new ContactUI();
    DataSet dsCategoryPage;

    protected void Page_Load(object sender, EventArgs e)
    {
        dsCategoryPage = objCategoryPage.GetAll();
        if (!IsPostBack)
        {
            BindCategoryPage();
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }


    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //bien page-alias
        string alias = Utils.Format.ConvertToNoSign(txtContactName.Text.Trim());
        if (txtContactID.Value.Trim() == "")
        {
            try
            {
                int result = objContact.Add(Convert.ToInt32(cboCategoryPage.SelectedValue), txtContactName.Text.Trim(), alias, txtContactImage.Text.Trim(), txtCompanyName.Text.Trim(), txtContactWebsite.Text.Trim(), txtContactEmail.Text.Trim(),
                    txtContactTelephone.Text.Trim(),txtContactMobilePhone.Text.Trim(), txtContactYahoo.Text.Trim(), txtContactSkype.Text.Trim(), txtContactCompanyAddress.Text.Trim(), txtContactTaxCode.Text.Trim(),
                    txtContactFax.Text.Trim(), txtContactAbstract.Text.Trim(), txtContactContent.Content.Trim(), txtContactPageTitle.Text.Trim(), 
                    txtMetaKeyword.Text, txtMetaDescription.Text, Convert.ToInt32(txtViewOrder.Text), chkIsTypical.Checked, false);

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                //lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
                lblMessage.Text = ex.ToString();
            }
        }
        else
        {
            try
            {

                int result = objContact.Update(Convert.ToInt32(txtContactID.Value.Trim()), Convert.ToInt32(cboCategoryPage.SelectedValue), txtContactName.Text.Trim(), alias, txtContactImage.Text.Trim(), txtCompanyName.Text.Trim(), txtContactWebsite.Text.Trim(), txtContactEmail.Text.Trim(),
                    txtContactTelephone.Text.Trim(), txtContactMobilePhone.Text.Trim(), txtContactYahoo.Text.Trim(), txtContactSkype.Text.Trim(), txtContactCompanyAddress.Text.Trim(), txtContactTaxCode.Text.Trim(), 
                    txtContactFax.Text.Trim(), txtContactAbstract.Text.Trim(), txtContactContent.Content.Trim(), txtContactPageTitle.Text.Trim(),
                    txtMetaKeyword.Text, txtMetaDescription.Text, Convert.ToInt32(txtViewOrder.Text), chkIsTypical.Checked, false);

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/quan-tri-noi-dung/quan-tri-doi-tac.aspx");
    }

    #region Cac ham tuong tac du lieu

    private void BindCategoryPage()
    {
        try
        {
            cboCategoryPage.DataSource = TableLib.CreateTable(dsCategoryPage.Tables[0], "FunctionPageAlias = 'khach-hang-thiet-ke-website'");
            cboCategoryPage.DataBind();
        }
        catch (Exception e)
        {

        }
    }

    private void LoadEditData(int ContactID)
    {
        DataSet ds = objContact.GetByID(ContactID);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtContactID.Value = row["ContactID"].ToString();

            txtMetaDescription.Text = row["MetaDescription"].ToString();
            txtMetaKeyword.Text = row["MetaKeyword"].ToString();
            txtContactPageTitle.Text = row["ContactPageTitle"].ToString();
            txtContactName.Text = row["ContactName"].ToString();
            txtContactWebsite.Text = row["ContactWebsite"].ToString();
            //txtContactCompanyAddress.Text = row["ContactCompanyAddress"].ToString();

            /*txtCompanyName.Text = row["ContactCompanyName"].ToString();
            txtContactAbstract.Text = row["ContactAbstract"].ToString();
            txtContactContent.Content = row["ContactContent"].ToString();
            
            txtContactEmail.Text = row["ContactEmail"].ToString();
            txtContactFax.Text = row["ContactFax"].ToString();
            txtContactImage.Text = row["ContactImage"].ToString();
            txtContactTaxCode.Text = row["ContactTaxCode"].ToString();
            txtContactYahoo.Text = row["ContactYahoo"].ToString();
            txtContactSkype.Text = row["ContactSkype"].ToString();
            
            txtViewOrder.Text = row["ViewOrder"].ToString();
            
            txtContactTelephone.Text = row["ViewOrder"].ToString();
            chkIsTypical.Checked = Convert.ToBoolean(row["IsTypical"]);*/

            General.SetSelectedItemInComboBox(row["CategoryPageID"].ToString(), cboCategoryPage);
        }
    }

    private void ReInsertData()
    {
        txtContactID.Value = "";

        txtMetaDescription.Text = "";
        txtMetaKeyword.Text = "";
        txtCompanyName.Text = "";
        txtContactPageTitle.Text = "";
        txtContactAbstract.Text = "";
        txtContactContent.Content = "";
        txtContactWebsite.Text = "";
        txtContactEmail.Text = "";
        txtContactFax.Text = "";
        txtContactImage.Text = "";
        txtContactMobilePhone.Text = "";
        txtContactName.Text = "";
        txtContactTelephone.Text = "";
        txtContactTaxCode.Text = "";
        txtContactYahoo.Text = "";
        txtContactSkype.Text = "";
        txtContactCompanyAddress.Text = "";
        chkIsTypical.Checked = false;
        txtViewOrder.Value = 0;

        General.SetSelectedItemInComboBox(null, cboCategoryPage);
    }

    #endregion
}
